/**
 * Copyright © 2011 looty - Textoo
 * @link http://www.textoo.org
 * @link http://code.google.com/p/textoo/
 * @author lab9 - Bertrand Larrieu
 * @mail lab9.fr@gmail.com
 * @version 2.0
 */

package org.looty.textoo.context 
{
	
	import flash.errors.*;
	import flash.events.*;
	import flash.system.*;
	import flash.text.*;
	import org.looty.core.textoo.context.local.*;
	import org.looty.core.textoo.context.style.*;
	import org.looty.core.textoo.format.*;
	import org.looty.core.textoo.render.*;
	import org.looty.log.*;
	import org.looty.textoo.*;
	import org.looty.textoo.events.*;
	import org.looty.textoo.render.*;
	
	/**
	 * Localisation langage code is built from the langage code defined in the <a href="http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes" >ISO 639</a> standard along with the country code defined in the <a href="http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2" >ISO 3166</a>. eg : en_UK, fr_FR
	 */
	
	public class TextooContext extends EventDispatcher implements ITextooContext
	{
		
		private var _styleManager					:StyleManager;
		private var _locManager						:LocalisationManager;
		private var _langManager					:LanguageManager;
		private var _formater						:RichTextFormater;
		private var _renderers						:Vector.<ITextooRenderer>;
		
		static private const FORMAT_EVENT			:TextooEvent = new TextooEvent(TextooEvent.REQUIRE_FORMAT);
		
		static private const RENDERERS_DEFINITIONS	:Vector.<String> = Vector.<String>(["org.looty.core.textoo.render.TextFieldRenderer", "org.looty.core.textoo.render.BitmapRenderer", "org.looty.core.textoo.render.SplittedTextfieldRenderer", "org.looty.core.textoo.render.SplittedBitmapRenderer"]);
			
		public function TextooContext() 
		{			
			_styleManager = new StyleManager();
			_locManager = new LocalisationManager();
			_langManager = new LanguageManager();
			_formater = new RichTextFormater();
			_renderers = new Vector.<ITextooRenderer>(RENDERERS_DEFINITIONS.length, true);
			
			_langManager.addEventListener(TextooEvent.LANGUAGE_ADDED, handleLanguageAdded);
			_langManager.addEventListener(TextooEvent.LANGUAGE_CHANGE, handleLanguageChange);			
			_styleManager.addEventListener(TextooEvent.STYLESHEET_CHANGE, handleStyleChange);
			_locManager.addEventListener(TextooEvent.LOCALISATION_ADDED, handleEvent);
		}
		
		private function createRenderer(id:uint):Boolean
		{
			if (!ApplicationDomain.currentDomain.hasDefinition(RENDERERS_DEFINITIONS[id])) 
			{
				var msg:String = RENDERERS_DEFINITIONS[id] + " not injected. Please check org.looty.textoo.render.RendererInjection";
				Looger.fatal(msg);
				throw new IllegalOperationError(msg);
				return false;
			}
			
			var definition:Class = ApplicationDomain.currentDomain.getDefinition(RENDERERS_DEFINITIONS[id]) as Class;
			var renderer:ITextooRenderer = new definition() as ITextooRenderer;
			
			_renderers[id] = renderer;
			
			return true;
		}
		
		private function handleEvent(e:TextooEvent):void
		{
			dispatchEvent(e);
		}		
		
		private function handleStyleChange(e:TextooEvent):void 
		{
			_formater.styleSheet = _styleManager.styleSheet;
			dispatchEvent(FORMAT_EVENT);
		}
		
		private function handleLanguageChange(e:TextooEvent):void 
		{
			_locManager.index = _langManager.index;
			_styleManager.index = _langManager.index;
		}		
		
		private function handleLanguageAdded(e:TextooEvent):void 
		{
			_locManager.setLength(_langManager.length);
			_styleManager.setLength(_langManager.length);
		}		
		
		public function addStyleSheet(CSSText:String, lang:String = "", title:String = "preferred"):void 
		{
			Looger.info("add styleSheet [ language : " + lang + " - title : " + title + " ]");
			_styleManager.addStyleSheet(CSSText, _langManager.add(lang), title);
		}
		
		public function get styleSheet():StyleSheet
		{
			return _styleManager.styleSheet;
		}		
		
		public function addLocalText(key:String, lang:String, text:String, className:String = ""):void 
		{
			_locManager.addText(key, _langManager.add(lang), text, className != null ? className : "");
		}
		
		public function setLocalTextDefaultClass(key:String, className:String):void
		{
			_locManager.setDefaultClass(key, className);
		}
		
		public function getText(key:String):LocalisationText
		{
			return _locManager.getText(key);
		}
		
		public function detectLanguage():void
		{
			_langManager.detectLanguage();
		}
		
		public function set language(value:String):void
		{
			_langManager.setLanguage(value);
		}
		
		public function get language():String { return _langManager.language; }
		
		public function get languages():Vector.<String> { return _langManager.languages;  }		
		
		public function format(props:TextooProps):void 
		{
			_formater.format(props);
		}
		
		public function render(props:TextooProps):RenderData 
		{			
			if (_renderers[props.rendererId] == null) createRenderer(props.rendererId);
			
			return _renderers[props.rendererId].render(props);
		}
		
		public function dispose():void
		{			
			_langManager.removeEventListener(TextooEvent.LANGUAGE_ADDED, handleLanguageAdded);
			_langManager.removeEventListener(TextooEvent.LANGUAGE_CHANGE, handleLanguageChange);			
			_styleManager.removeEventListener(TextooEvent.STYLESHEET_CHANGE, handleStyleChange);
			_locManager.removeEventListener(TextooEvent.LOCALISATION_ADDED, handleEvent);
			
			_locManager.dispose();
			_styleManager.dispose();
			_langManager.dispose();
			
			for each (var renderer:ITextooRenderer in _renderers) if (renderer != null) renderer.dispose();
			_renderers.length = 0;
			_renderers = null;
		}
		
		public function dump():void
		{
			_langManager.dump();
			_locManager.dump();
			_styleManager.dump();
		}		
		
	}
	
	RendererInjection;
}